/**
 * 
 */

package com.tenentia.pcmps.swm.web.jsf.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 * @author tEnEntia, S.L.N.E.
 * 
 */
public class NIFValidator implements Validator {

	public static final String NIF_CHARS = "TRWAGMYFPDXBNJZSQVHLCKET";

	public static final String VALIDATION_MESSAGE = "El DNI/NIF no es correcto.";

	/**
	 * Constructor por defecto.
	 */
	public NIFValidator() {

		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.faces.validator.Validator#validate(javax.faces.context.FacesContext, javax.faces.component.UIComponent, java.lang.Object)
	 */
	@Override
	public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) throws ValidatorException {

		boolean isRequired = false;

		String uiComponentLabel = null;

		try {
			HtmlInputText tmpInput = (HtmlInputText) uiComponent;
			isRequired = tmpInput.isRequired();
			uiComponentLabel = tmpInput.getLabel();
			if (null != uiComponentLabel) {
				uiComponentLabel = uiComponentLabel.trim();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}

		if (null == value) {
			if (isRequired) {
				throw new ValidatorException(new FacesMessage(null == uiComponentLabel ? NIFValidator.VALIDATION_MESSAGE : uiComponentLabel + ": " + NIFValidator.VALIDATION_MESSAGE));
			} else {
				return;
			}
		} else {
			try {
				String tmpString = (String) value;
				tmpString = tmpString.toUpperCase();
				int tmpInt = tmpString.length();

				if (tmpInt == 0) {
					if (isRequired) {
						throw new ValidatorException(new FacesMessage(null == uiComponentLabel ? NIFValidator.VALIDATION_MESSAGE : uiComponentLabel + ": " + NIFValidator.VALIDATION_MESSAGE));
					} else {
						return;
					}
				} else {
					if (tmpInt > 9 || tmpInt < 2) {
						throw new ValidatorException(new FacesMessage(null == uiComponentLabel ? NIFValidator.VALIDATION_MESSAGE : uiComponentLabel + ": " + NIFValidator.VALIDATION_MESSAGE));
					} else {
						if (tmpString.matches("^(\\d{1,8}[TRWAGMYFPDXBNJZSQVHLCKET])$")) {

							char tmpChar = tmpString.charAt(tmpInt - 1);
							tmpString = tmpString.substring(0, tmpInt - 1);
							tmpInt = Integer.parseInt(tmpString);
							tmpInt = tmpInt % 23;
							if (NIF_CHARS.charAt(tmpInt) == tmpChar) {
								return;
							} else {
								throw new ValidatorException(new FacesMessage(null == uiComponentLabel ? NIFValidator.VALIDATION_MESSAGE : uiComponentLabel + ": " + NIFValidator.VALIDATION_MESSAGE));
							}
						} else {
							throw new ValidatorException(new FacesMessage(null == uiComponentLabel ? NIFValidator.VALIDATION_MESSAGE : uiComponentLabel + ": " + NIFValidator.VALIDATION_MESSAGE));
						}
					}
				}
			} catch (Exception e) {
				throw new ValidatorException(new FacesMessage(null == uiComponentLabel ? NIFValidator.VALIDATION_MESSAGE : uiComponentLabel + ": " + NIFValidator.VALIDATION_MESSAGE));
			}
		}
	}
}
